/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.editor;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;

/**
 * Provides standard fonts for diagrams. Each font is identified with a 
 * keyword so that the user can select fonts in the preference pages.
 * 
 * <p>Each call to {@link #getFont()} should be paired with a call to
 * {@link #releaseFont()}.</p>
 * 
 * @todo Move to the Common plugin 
 *       Or even better: find a class in the Eclipse API that offers the 
 *       same service. 
 * 
 * @author frankd@ifi.uio.no
 */
public class FontRegistry {
	private static Font font;
	private static int count;
	
	/**
	 * Returns the best default font, or null if none of the desired
	 * fonts could be found on the system. In that case, the system
	 * default font should take effect.
	 */
	public static Font getFont() {
		if (count++ == 0) {
			String[] families = { "Tahoma", "Arial" };
			for (int i = 0; i < families.length && font == null; ++i) {
				try {
					font = new Font(null, families[i], 8, SWT.NORMAL);
				}
				catch (Throwable e) {
					// ignore and try next font
				}
			}
		}
		return font;
	}
	
	public static void releaseFont() {
		if (--count == 0 && font != null) {
			font.dispose();
			font = null;
		}
	}
}
